<!-- 运费管理 -->
<template>
  <div class="dashboard-container waybill-list customer-list-box">
    <div class="app-container">
      <el-card shadow="never">
        <el-button
          type="warning"
          @click="dialogVisible = true"
        >新增模板</el-button>
        <el-table
          :data="freightList"
          style="width: 100%"
          stripe
        >
          <el-table-column
            label="模板类型"
          >
            <template #default="scope">
              {{ formatTemplateType(scope.row.templateType) }}
            </template>
          </el-table-column>
          <el-table-column
            label="运送类型"
          >
            <template #default="scope">
              {{ formatTransportType(scope.row.transportType) }}
            </template>
          </el-table-column>
          <el-table-column
            label="关联城市"
            prop="associatedCityList"
          >
          </el-table-column>
          <el-table-column
            label="首重"
            prop="firstWeight"
          >
          </el-table-column>
          <el-table-column
            label="续重"
            prop="continuousWeight"
          >
          </el-table-column>
          <el-table-column
            label="轻抛系数"
            prop="lightThrowingCoefficient"
          >
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="editFreight(scope.row)"
              >编辑</el-button>
              <span>|</span>
              <el-button
                size="mini"
                type="text"
                style="color: #F56C6C;"
                @click="delfreight(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span slot="title">{{ freightForm.id ? '编辑模板' : '新增模板' }}</span>
      <el-form
        ref="form"
        :model="freightForm"
        :rules="freightRules"
        label-width="100px"
      >
        <el-form-item
          label="模板类型"
          prop="templateType"
        >
          <el-select
            v-model="freightForm.templateType"
            placeholder="请选择模板类型"
          >
            <el-option
              v-for="item in templateTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="运送类型"
          prop="transportType"
        >
          <el-select
            v-model="freightForm.transportType"
            placeholder="请选择运送类型"
          >
            <el-option
              v-for="item in transportTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="运送类型"
          prop="associatedCityList"
        >
          <el-checkbox-group
            v-if="freightForm.id"
            v-model="freightForm.associatedCityList"
          >
            <el-checkbox label="京津冀"></el-checkbox>
            <el-checkbox label="江浙沪"></el-checkbox>
            <el-checkbox label="川渝"></el-checkbox>
            <el-checkbox label="黑吉辽"></el-checkbox>
          </el-checkbox-group>
          <span v-if="!freightForm.id">全国</span>
        </el-form-item>
        <el-form-item
          label="首重价格"
          prop="firstWeight"
        >
          <el-input
            v-model="freightForm.firstWeight"
            placeholder="请输入首重价格"
          >
            <template slot="suffix">元</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="续重价格"
          prop="continuousWeight"
        >
          <el-input
            v-model="freightForm.continuousWeight"
            placeholder="请输入续重价格"
          >
            <template slot="suffix">元</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="轻抛系数"
          prop="lightThrowingCoefficient"
        >
          <el-input
            v-model="freightForm.lightThrowingCoefficient"
            placeholder="请输入轻抛系数"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="warning"
          @click="addfreightManage"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { freightManageList, freightManageOperate, freightManageDelete } from '@/api/transit'
export default {
  data() {
    return {
      freightList: [],
      dialogVisible: false,
      templateTypeOptions: [
        {
          value: 1,
          label: '同城寄'
        },
        {
          value: 2,
          label: '省内寄'
        },
        {
          value: 3,
          label: '经济区互寄'
        },
        {
          value: 4,
          label: '跨省'
        }
      ],
      transportTypeOptions: [
        {
          value: 1,
          label: '普快'
        },
        {
          value: 2,
          label: '特快'
        }
      ],
      freightForm: {
        associatedCityList: ['全国'],
        continuousWeight: '',
        firstWeight: '',
        templateType: '',
        transportType: '',
        lightThrowingCoefficient: ''
      },
      freightRules: {
        associatedCityList: [
          { required: true, message: '请选择运送类型', trigger: 'blur' }
        ],
        continuousWeight: [
          { required: true, message: '请输入续重价格', trigger: 'blur' }
        ],
        firstWeight: [
          { required: true, message: '请输入首重价格', trigger: 'blur' }
        ],
        templateType: [
          { required: true, message: '请选择模板类型', trigger: 'blur' }
        ],
        transportType: [
          { required: true, message: '请选择运送类型', trigger: 'blur' }
        ],
        lightThrowingCoefficient: [
          { required: true, message: '请输入轻抛系数', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.freightManage()
  },
  methods: {
    async freightManage () {
      const res = await freightManageList()
      this.freightList = res.data
    },
    // 模板类型&运送类型代码转文字
    formatTemplateType(type) {
      const TypeMap = {
        1: '同城寄',
        2: '省内寄',
        3: '经济区互寄',
        4: '跨省'
      }
      return TypeMap[type]
    },
    formatTransportType(type) {
      const TypeMap = {
        1: '普快',
        2: '特快'
      }
      return TypeMap[type]
    },
    // 新增运费弹框逻辑
    handleClose() {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible = false
          this.$refs.form.resetFields()
          this.freightForm = {
            associatedCityList: [1],
            continuousWeight: '',
            firstWeight: '',
            templateType: '',
            transportType: '',
            lightThrowingCoefficient: ''
          }
        })
        .catch(_ => {})
    },
    addfreightManage() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // todo
          const res = await freightManageOperate(this.freightForm)
          if (res.code !== 200) {
            this.$message.warning(res.msg)
          } else {
            this.$message.success(this.freightForm.id ? '成功修改模板' : '成功新增模板')
          }
          this.dialogVisible = false
          this.freightManage()
        }
      })
    },
    editFreight(obj) {
      console.log(obj)
      this.freightForm = obj
      this.dialogVisible = true
    },
    titleText() {
      if (this.freightForm.id) {
        return '编辑模板'
      } else {
        return '新增模板'
      }
    },
    delfreight(id) {
      this.$confirm('确定要删除吗？', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await freightManageDelete(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.freightManage()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped rel="stylesheet/scss">
.waybill-list {
  .el-dialog__wrapper {
    ::v-deep .el-dialog__body {
      padding-left: 0px;
    }
  }

  .el-table {
    margin-top: 35px;
  }
}
</style>
